package com.baibei.order.list;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.baibei.model.Area;
import com.baibei.model.BuyDirection;
import com.baibei.model.OrderInfo;
import com.baibei.module.AreaHelper;
import com.baibei.module.GlideApp;
import com.baibei.order.R;
import com.baibei.order.R2;
import com.baibei.order.emptyview.EmptyAdapter;
import com.baibei.order.emptyview.EmptyResourceInfo;
import com.baibei.order.emptyview.EmptyViewHolder;
import com.baibei.widget.PriceTextView;
import com.blankj.utilcode.utils.ConvertUtils;
import com.rae.swift.Rx;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 订单列表适配器
 * Created by shenminjie on 2017/8/10.
 * mail:shenminjie@100bei.com
 */

class IndexOrderAdapter extends EmptyAdapter {

    /**
     * 回调监听
     */
    private OnItemSelectListener mListener;

    /**
     * 数据源
     */
    private List<OrderInfo> mOrderInfos;


    IndexOrderAdapter(OnItemSelectListener listener) {
        this.mListener = listener;
        isOpenEmptyView(true);
    }

    /**
     * 设置订单数据
     *
     * @param infos infos
     */
    void setOrderList(List<OrderInfo> infos) {
        mOrderInfos = infos;
    }

    /**
     * 获取数据源
     *
     * @return return
     */
    List<OrderInfo> getOrderList() {
        return mOrderInfos;
    }


    @Override
    public int getCount() {
        return mOrderInfos == null ? 0 : mOrderInfos.size();
    }

    @Override
    public RecyclerView.ViewHolder onCreateItemViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_index_order, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ViewHolder) {
            ViewHolder viewHolder = (ViewHolder) holder;
            final OrderInfo info = mOrderInfos.get(position);
            //商品名称
            viewHolder.tvProductName.setText(info.getName());
            //订货类型
            viewHolder.tvType.setText(info.getBuydirection() == BuyDirection.DIRECTION_CURRENT_PRICE ? R.string.current_price_order : R.string.settle_price_order);
            //logo
            GlideApp.with(viewHolder.ivProduct).load(info.getProductpic()).placeholder(R.color.dividerColor).into(viewHolder.ivProduct);
            //订货价(元)
            viewHolder.tvOrderPrice.setText(Rx.formatPrice(info.getBuyprice()));
            //最新价
            viewHolder.tvLastPrice.setText(Rx.formatPrice(info.getLast()));
            //订货量(件)
            viewHolder.tvOrderCount.setText(String.valueOf(info.getCount()));
            //差价合计
            viewHolder.tvTotalPrice.setPrice(info.getPrifitDifferent());
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        mListener.onItemSelectListener(view, info, position);
                    }
                }
            });
            viewHolder.btnUnsubscribe.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        mListener.onUnsubscribeBtnClickListener(view, info, position);
                    }
                }
            });

            viewHolder.ivFlag.setImageResource(AreaHelper.getImageResource(Area.valueOf(info.getArea())));
        }
    }

    @Override
    public void onBindEmptyViewHolder(EmptyViewHolder holder) {
        super.onBindEmptyViewHolder(holder);
        ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
        params.height = ConvertUtils.dp2px(300);
        holder.itemView.setLayoutParams(params);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R2.id.iv_product)
        ImageView ivProduct;
        @BindView(R2.id.iv_flag)
        ImageView ivFlag;
        @BindView(R2.id.tv_product_name)
        TextView tvProductName;
        @BindView(R2.id.tv_type)
        TextView tvType;
        @BindView(R2.id.tv_order_price)
        TextView tvOrderPrice;
        @BindView(R2.id.tv_last_price)
        TextView tvLastPrice;
        @BindView(R2.id.tv_order_count)
        TextView tvOrderCount;
        @BindView(R2.id.view_divider)
        View viewDivider;
        @BindView(R2.id.tv_total_price)
        PriceTextView tvTotalPrice;
        @BindView(R2.id.btn_unsubscribe)
        TextView btnUnsubscribe;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    @Override
    public EmptyResourceInfo getEmptyResourceInfo() {
        EmptyResourceInfo info = super.getEmptyResourceInfo();
        info.setListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener != null)
                    mListener.onReload();
            }
        });
        return info;
    }

    /**
     * 回调接口
     */
    interface OnItemSelectListener extends com.baibei.module.OnItemSelectListener<OrderInfo> {

        /**
         * 退订回调操作
         *
         * @param view     view
         * @param info     info
         * @param position position
         */
        void onUnsubscribeBtnClickListener(View view, OrderInfo info, int position);

        /**
         * 重新加载操作
         */
        void onReload();
    }

}
